package com.xz.stu.server.service.impl;

import cn.hutool.core.util.PageUtil;
import cn.iocoder.yudao.framework.common.exception.ErrorCode;
import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.framework.common.util.object.PageUtils;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import com.alibaba.druid.sql.PagerUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xz.stu.server.entity.Enrollment;
import com.xz.stu.server.service.EnrollmentService;
import com.xz.stu.server.mapper.EnrollmentMapper;
import com.xz.stu.server.vo.grade.GradeByTermVo;
import com.xz.stu.server.vo.grade.GradePageVo;
import com.xz.stu.server.vo.grade.GradeStudent;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
* @author 熊磊
* @description 针对表【school_enrollment】的数据库操作Service实现
* @createDate 2025-04-04 15:49:12
*/
@Service
public class EnrollmentServiceImpl extends ServiceImpl<EnrollmentMapper, Enrollment>
    implements EnrollmentService{
    @Resource
    private EnrollmentMapper enrollmentMapper;

    @Override
    public PageResult<GradeStudent> getGrade(GradePageVo gradePageVo) {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        if (userId == null){
            throw new ServiceException(new ErrorCode(1000000000,"用户未登录"));
        }
        List<GradeStudent> gradeStudents = enrollmentMapper.getGrade(userId, gradePageVo.getTermId());
        //分页
        int start = PageUtils.getStart(gradePageVo);
        int end = Math.min((start + gradePageVo.getPageSize()), gradeStudents.size());
        return new PageResult<>(gradeStudents.subList(start, end), (long) gradeStudents.size());
    }

    @Override
    public List<GradeByTermVo> getGradeByTerm() {
        return enrollmentMapper.getgetGradeByTerm();
    }
}




